export function isEmpty(obj) {
  if (!obj) return;
  return Object.keys(obj).length === 0 && obj.constructor === Object;
}

/**
 * 根据id 删除数组某一项
 * @param {*} target
 * @param {*} id
 */
export function deleteArrById(target, id) {
  const arr = [...target];
  if (!arr || !id) {
    return [];
  }
  let i;
  for (var j = 0; j < arr.length; j++) {
    if (arr[j].id === id) {
      i = j;
    }
  }
  arr.splice(i, 1);
  return arr;
}
/**
 * 根据id 删除数组某一项
 * @param {*} target
 * @param {*} id
 */
export function deleteArrById2(target, id) {
  const arr = [...target];
  if (!arr || !id) {
    return [];
  }
  let i;
  for (var j = 0; j < arr.length; j++) {
    if (arr[j] === id) {
      i = j;
    }
  }
  arr.splice(i, 1);
  return arr;
}
/**
 * 根据param_id删除数组某一项
 * @param {*} target
 * @param {*} id
 */
export function deleteArrById3(target, id) {
  const arr = [...target];
  if (!arr || !id) {
    return [];
  }
  let i;
  for (var j = 0; j < arr.length; j++) {
    if (arr[j].param_id === id) {
      i = j;
    }
  }
  arr.splice(i, 1);
  return arr;
}
/**
 * 根据一个数组的顺序排序
 * @param {*} target1
 * @param {*} target2
 */
export function sortByArr(target1, target2) {
  const arr1 = [...target1];
  const arr2 = [...target2];
  if (!arr1 || !arr2) {
    return [];
  }
  arr1.forEach((item) => {
    let sortId = arr2.indexOf(item.id);
    item.sortId = sortId;
  });
  let new_arr1 = arr1.sort(sort("sortId"));
  return new_arr1;
}
function sort(prop) {
  return function (obj1, obj2) {
    var val1 = obj1[prop];
    var val2 = obj2[prop];
    if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
      val1 = Number(val1);
      val2 = Number(val2);
    }
    if (val1 < val2) {
      return -1;
    } else if (val1 > val2) {
      return 1;
    } else {
      return 0;
    }
  };
}
/*
 * 将base64转换成可用formdata提交的文件
 * @param {String} urlData base64的url
 */
export function convertBase64UrlToBlob(urlData) {
  //去掉url的头，并转换为byte
  var bytes = window.atob(urlData.split(",")[1]);
  //处理异常,将ascii码小于0的转换为大于0
  var ab = new ArrayBuffer(bytes.length);
  var ia = new Uint8Array(ab);
  for (var i = 0; i < bytes.length; i++) {
    ia[i] = bytes.charCodeAt(i);
  }
  return new Blob([ab], { type: "image/bmp" });
}

export function convertBase64UrlToBlob2(urlData) {
  //去掉url的头，并转换为byte
  var bytes = window.atob(urlData.split(",")[1]);
  //处理异常,将ascii码小于0的转换为大于0
  var ab = new ArrayBuffer(bytes.length);
  var ia = new Uint8Array(ab);
  for (var i = 0; i < bytes.length; i++) {
    ia[i] = bytes.charCodeAt(i);
  }
  return new Blob([ab], { type: "image/png" });
}
export function convertBase64UrlToBlob3(urlData) {
  //去掉url的头，并转换为byte
  var bytes = window.atob(urlData.split(",")[1]);
  //处理异常,将ascii码小于0的转换为大于0
  var ab = new ArrayBuffer(bytes.length);
  var ia = new Uint8Array(ab);
  for (var i = 0; i < bytes.length; i++) {
    ia[i] = bytes.charCodeAt(i);
  }
  return new Blob([ab], { type: "image/jpg" });
}

//传入图片路径，返回base64
export function getBase64(img) {
  return new Promise((resolve) => {
    var image = new Image();
    image.src = img;
    if (img) {
      image.onload = function () {
        resolve(_getBase64Image(image));
      };
    }
  });
}

function _getBase64Image(img, width, height) {
  var canvas = document.createElement("canvas");
  canvas.width = width ? width : img.width;
  canvas.height = height ? height : img.height;
  var ctx = canvas.getContext("2d");
  ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
  var dataURL = canvas.toDataURL("image/bmp", 1);
  return dataURL;
}

//找到两个数组中最大值
export function foundMaxVal(target1, target2) {
  if (target1.length == 0 && target2.length == 0) {
    return 0;
  } else if (target1.length > 0 && target2.length > 0) {
    if (target1[0].index && target2.index) {
      const arr = [...target1, ...target2].sort((a, b) => a.index - b.index);
      return arr[arr.length - 1].index;
    } else {
      const arr = [...target1, ...target2];
      return arr.length - 1;
    }
  } else if (target1.length == 0 && target2.length > 0) {
    if (target2[0].index) {
      const arr = [...target2].sort((a, b) => a.index - b.index);
      return arr[arr.length - 1].index;
    } else {
      return target2.length;
    }
  } else if (target1.length > 0 && target2.length == 0) {
    if (target1[0].index) {
      const arr = [...target1].sort((a, b) => a.index - b.index);
      return arr[arr.length - 1].index;
    } else {
      return target1.length;
    }
  }
}

//延时函数
export function delayDate(time) {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log("开始执行");
      resolve("success");
    }, time);
  });
}
//防抖函数
export function debounce(func, delay) {
  let timer = null;
  return function (...args) {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      func.apply(this, args);
    }, delay);
  };
}
export function isJSON(str) {
  if (typeof str == "string") {
    try {
      var obj = JSON.parse(str);
      if (typeof obj == "object" && obj) {
        return true;
      } else {
        return false;
      }
    } catch (e) {
      return false;
    }
  }
}
export function unique(arr) {
  let find = [];
  arr.forEach((el) => {
    if (!find.find((e) => e.id == el.id)) {
      find.push(el);
    }
  });
  return find;
}
export function unique2(arr) {
  let find = [];
  arr.forEach((el) => {
    if (!find.find((e) => e.param_id == el.param_id)) {
      find.push(el);
    }
  });
  return find;
}
//px转换为mm
export function px2mm(px) {
  var ratio = 0;
  var div = document.createElement("div");
  div.style.width = "1cm";
  div.id = "puc";
  document.body.appendChild(div);
  var w = getComputedStyle(div, null).width;
  ratio = w.substr(0, w.length - 2);
  div.parentNode.removeChild(div);
  var r = ratio / 10;
  return Math.round(px / r);
}
//px转换为cm
export function px2cm(px) {
  var ratio = 0;
  var div = document.createElement("div");
  div.style.width = "1cm";
  div.id = "puc";
  document.body.appendChild(div);
  var w = getComputedStyle(div, null).width;
  ratio = w.substr(0, w.length - 2);
  div.parentNode.removeChild(div);
  return Math.round((px * 10) / ratio) / 10;
}
//屏幕上两点的距离
export function distance(x, y) {
  if (!x || !y) {
    return 0;
  }
  const dx = Math.abs(x.x - y.x);
  const dy = Math.abs(x.y - y.y);
  const dis = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  return dis;
}

//根据url获取name
export function getNameByUrl(url, type = ".jpg") {
  if (!url) return;
  const url_arr = url.split("/");
  const url_len = url_arr.length;
  const name_i = url_arr[url_len - 1];
  const name_arr = name_i.split(type);
  return name_arr[0];
}

//颈动脉
export const scanResult3 = {
  15: "LeftCrossCCA", //左 侧颈总动脉横切面      Show_deviceParam
  16: "LeftCrossBifurcation", //左 窦部
  17: "LeftCrossICAECA", //左 侧颈内及颈外动脉横切面
  18: "LeftLongCCAStartSeg", //左 侧颈总动脉起始部纵切面
  19: "LeftLongCCA", //左 侧颈总动脉纵切面
  20: "LeftLongBifurcation",
  21: "LeftLongICA", //左 侧颈内及颈外动脉纵切面
  22: "LeftLongECA", //左 侧颈内及颈外动脉纵切面
  23: "LeftLongColorCCA", //
  24: "LeftLongColorICA", //
  25: "LeftLongColorECA", //
  26: "LeftLongPWCCA", //
  27: "LeftLongPWICA", //
  28: "LeftLongPWECA", //
  29: "RightCrossCCA", //右 侧颈总动脉横切面
  30: "RightCrossBifurcation", //右 窦部
  31: "RightCrossICAECA", // 右 侧颈内及颈外动脉横切面
  32: "RightLongCCAStartSeg", //右 侧颈总动脉起始部纵切面
  33: "RightLongCCA", //右 侧颈总动脉纵切面
  34: "RightLongBifurcation", //
  35: "RightLongICA", // 右 侧颈内及颈外动脉纵切面
  36: "RightLongECA", // 右 侧颈内及颈外动脉纵切面
  37: "RightLongColorCCA",
  38: "RightLongColorICA",
  39: "RightLongColorECA",
  40: "RightLongPWCCA",
  41: "RightLongPWICA",
  42: "RightLongPWECA",
};
//颈动脉扫查结果转换成中文
export const artery2C = {
  15: "-颈总动脉横切面", //左 侧颈总动脉横切面      Show_deviceParam
  16: "-窦部横切面", //左 侧窦部横切面
  17: "-颈内及颈外动脉横切面", //左 侧颈内及颈外动脉横切面
  18: "-颈总动脉起始部纵切面", //左 侧颈总动脉起始部纵切面
  19: "-颈总动脉纵切面", //左 侧颈总动脉纵切面
  20: "-纵切窦部",
  21: "-颈内及颈外动脉纵切面", //左 侧颈内及颈外动脉纵切面
  22: "-颈内及颈外动脉纵切面", //左 侧颈内及颈外动脉纵切面
  23: "-颈总C模式", //
  24: "-颈内外C模式", //
  25: "-颈内外C模式", //
  26: "-颈总PW模式", //
  27: "-颈内外PW模式", //
  28: "-颈内外PW模式", //
  29: "-颈总动脉横切面", //右 侧颈总动脉横切面
  30: "-窦部横切面", //右 侧窦部横切面
  31: "-颈内及颈外动脉横切面", // 右 侧颈内及颈外动脉横切面
  32: "-颈总动脉起始部纵切面", //右 侧颈总动脉起始部纵切面
  33: "-颈总动脉纵切面", //右 侧颈总动脉纵切面
  34: "-纵切窦部", //
  35: "-颈内及颈外动脉纵切面", // 右 侧颈内及颈外动脉纵切面
  36: "-颈内及颈外动脉纵切面", // 右 侧颈内及颈外动脉纵切面
  37: "-颈总C模式",
  38: "-颈内外C模式",
  39: "-颈内外C模式",
  40: "-颈总PW模式",
  41: "-颈内外PW模式",
  42: "-颈内外PW模式",
  51: "横切颈总",
  52: "横切窦部",
  53: "横切颈内外",
  54: "纵切窦部",
  55: "纵切颈内外",
  56: "纵切颈总",
  57: "纵切起始部",
  61: "横切颈总",
  62: "横切窦部",
  63: "横切颈内外",
  64: "纵切窦部",
  65: "纵切颈内外",
  66: "纵切颈总",
  67: "纵切起始部",
};
//颈动脉扫查显示数据
export const showScanRes = [
  "15",
  "16",
  "17",
  "18",
  "19",
  "21",
  "22",
  "29",
  "30",
  "31",
  "32",
  "33",
  "35",
  "36",
];
//颈动脉 视频进度条对应部位
export const allowShowArteryPos = [
  "51",
  "52",
  "53",
  "54",
  "55",
  "56",
  "57",
  "61",
  "62",
  "63",
  "64",
  "65",
  "66",
  "67",
];
//智能匹配 16/20 21/22 30/34 35/36
export const artery_keys = [
  "16",
  "19",
  "20",
  "21",
  "22",
  "30",
  "33",
  "34",
  "35",
  "36",
];
export const artery_locs = [
  "IMT",
  "smooth",
  "plaque",
  "narrow",
  "loc",
  "shape",
  "echo_intensity",
  "echo_distribution",
  "diameter_narrow",
  "area_narrow",
  "RI",
  "length",
  "thickness",
];
export const artery_normal_default = {
  IMT: "未见内中膜增厚",
  smooth: "光滑",
  plaque: "无",
};
export const artery_default = {
  IMT: "未见内中膜增厚",
  smooth: "光滑",
  plaque: "无",
  narrow: "无",
  loc: "左叶上极",
  shape: "规则",
  echo_intensity: "等回声",
  echo_distribution: "均匀",
  diameter_narrow: "",
  area_narrow: "",
  RI: "",
};
//智能匹配 16/20 21/22 30/34 35/36
export const artery2E = {
  16: "LeftCrossBifurcation", //左 窦部
  19: "LeftLongCCA",
  20: "LeftCrossBifurcation",
  21: "LeftLongICA",
  22: "LeftLongICA",
  30: "RightCrossBifurcation",
  33: "RightLongCCA",
  34: "RightCrossBifurcation",
  35: "RightLongICA",
  36: "RightLongICA",
};
//颈动脉PSED
export const arteryPSEDres = {
  1: "LeftLongICA_PSED",
  2: "LeftLongCCA_PSED",
  3: "RightLongICA_PSED",
  4: "RightLongCCA_PSED",
  5: "LeftCrossBifurcation_PSED",
  6: "RightCrossBifurcation_PSED",
};
//颈动脉feature-管径
export const diameter2E = {
  16: "LeftCrossBifurcation_d", //左 窦部
  19: "LeftLongCCA_d",
  20: "LeftCrossBifurcation_d",
  21: "LeftLongICA_d",
  22: "LeftLongICA_d",
  30: "RightCrossBifurcation_d",
  33: "RightLongCCA_d",
  34: "RightCrossBifurcation_d",
  35: "RightLongICA_d",
  36: "RightLongICA_d",
};

//甲状腺标准切面
export const scanResult4 = {
  1: "crossThmus", //峡部横切
  2: "crossRightLobe", //右叶横切
  6: "LongRIghtLobeUp", //右叶纵切
  7: "LongRIghtLobe", //右叶纵切
  8: "CrossLesion", //病灶横切
  11: "LongLesion", //病灶纵切
  12: "crossLeftLobe", //左叶横切
  16: "LongLeftLobeUp", //左叶横切
  17: "LongLeftLobe", //左叶纵切
  21: "CrossLesion", //病灶横切
  24: "LongLesion", //病灶纵切
};
//甲状腺测量转换中文
export const thyroid2C = {
  CrossThmus: "峡部横切面",
  CrossRightLobe: "右侧叶横切面",
  CrossRightLobeC: "横切右侧甲状腺C模式",
  LongRightLobeUp: "右侧叶纵切面",
  LongRightLobeMid: "右侧叶纵切面",
  LongRightLobeDown: "右侧叶纵切面",
  CrossLesion: "病灶横切",
  CrossLesionC: "横切病灶C模式",
  LongLesion: "病灶纵切",
  LongLesionC: "纵切病灶C模式",
  CrossLeftLobe: "左侧叶横切面",
  CrossLeftLobeC: "横切左侧甲状腺C模式",
  LongLeftLobeUp: "左侧叶纵切面",
  LongLeftLobeMid: "左侧叶纵切面",
  LongLeftLobeDown: "左侧叶纵切面",
  LongRightLobeC: "纵切右侧甲状腺C模式",
  LongLeftLobeC: "纵切左侧甲状腺C模式",
  51: "峡部",
  52: "左叶横切",
  53: "左叶纵切",
  62: "右叶横切",
  63: "右叶纵切",
};
//甲状腺测量对应关系
export const thyroid2E = {
  0: "thyroid_0", //峡部
  1: "thyroid_1", //左叶  左右径
  2: "thyroid_2", //左叶  前后径
  3: "thyroid_3", //左叶  上下径
  4: "thyroid_4", //右叶  左右径
  5: "thyroid_5", //右叶  前后径
  6: "thyroid_6", //右叶 上下径
  7: "thyroid_7", //结节 左右径
  8: "thyroid_8", //结节 前后径
  9: "thyroid_9", //结节 上下径
};
export const thyroid_keys = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
export const thyroid_locs = [
  "loc",
  "shape",
  "aspect_ratio",
  "edge",
  "sonic_halo",
  "structure",
  "echo_intensity",
  "echo_distribution",
  "focal_hyperechoic",
  "rear_echo",
  "blood_flow",
  "jj_shape",
  "jj_echo_intensity",
  "jj_echo_distribution",
  "jjqhj",
  "jjzyj",
  "jjsxj",
];
export const thyroid_default = {
  loc: "左叶上极",
  shape: "规则",
  aspect_ratio: "A/T≥1",
  edge: "光整",
  sonic_halo: "无",
  structure: "实性",
  echo_intensity: "等回声",
  echo_distribution: "均匀",
  focal_hyperechoic: "无",
  rear_echo: "无改变",
  blood_flow: "无血供",
  jj_shape: "规则",
  jj_echo_intensity: "等回声",
  jj_echo_distribution: "均匀",
};
export const thyroid_normal_default = {
  shape: "规则",
  echo_intensity: "等回声",
  echo_distribution: "均匀",
};
export const allowShowThyroidPos = ["51", "52", "53", "62", "63"];
export const scanPostion = {
  normal: "正常",
  lesion: "疑似异常图",
  cali: "测量",
};
//肝脏扫查部位对应关系
export const liverPosRes = {
  3001: "LiverLongLL", //剑突下肝左叶纵切面
  3002: "LiverObliLRL", //剑突下左右肝斜切面
  3003: "LiverObliSecPortaAndIVC", //剑突下第二肝门及肝静脉
  3004: "LiverObliRLAndGB", //右肋第一肝门右肝斜切
  3005: "LiverLongRLAndRK", //右肋下右肝肾切面
  3006: "LiverLongGB", //胆囊长轴切面
  3007: "LiverPALong", //胰腺纵切面
};
//脾脏扫查部位对应关系
export const spleenPosRes = {
  4001: "SpleenLongSpAndSv", //脾脏和脾门静脉
};
//肝脾测量部位
export const digestive_keys = [
  "3011",
  "3012",
  "3021",
  "3051",
  "3031",
  "3032",
  "4011",
  "4012",
  "4021",
  "3061",
];
//肝脾测量部位对应特征组件的字段
export const digestive2E = {
  3011: "liver_11", //左叶纵切上下径
  3012: "liver_12", //左叶纵切厚径
  3021: "liver_21", //右叶斜径
  3031: "liver_31", //胆囊长径
  3032: "liver_32", //胆囊宽径
  3051: "liver_51", //门静脉主干内径
  4011: "spleen_4011", //脾脏长径
  4012: "spleen_4012", //脾脏厚径
  4021: "spleen_4021", //脾门静脉内径
  3061: "liver_36", //
};
//肾脏扫查部位对应关系
export const urinaryPosRes = {
  5011: "KidneyLeftLongLongAxis", //肾脏左侧长轴切面
  5012: "KidneyLeftLongLongAxisColor", //肾脏左侧长轴切面C模式
  5021: "KidneyRightLongLongAxis", //肾脏右侧长轴切面
  5022: "KidneyRightLongLongAxisColor", //肾脏右侧长轴切面C模式
};

export const urinary_keys = ["5011", "5012", "5021", "5022"];
//肾脏测量部位对应特征组件的字段

export const urinary2E = {
  5011: "urinary_5011", //左肾长径
  5012: "urinary_5012", //左肾宽径
  5021: "urinary_5021", //右肾长径
  5022: "urinary_5022", //右肾宽径
};

export const liver2C = {
  3001: "肝左叶经腹主动脉长轴纵切面",
  3002: "肝左叶斜切面(工字形结构)",
  3003: "肋下斜切第二肝门切面",
  3004: "右肋间经第一肝门斜切面",
  3005: "右肋下右肝及右肾纵切面",
  3006: "胆囊长轴纵切面",
  3007: "胰腺长轴切面",
  3008: "门静脉切面",
  3010: "肝左叶上下径",
  3011: "肝左叶上下径",
  3012: "肝左叶厚径",
  3020: "肝右叶斜径",
  3021: "肝右叶斜径",
  3030: "胆囊纵切长轴",
  3031: "胆囊长径",
  3032: "胆囊宽径",
  3040: "胆囊横切面",
  3041: "胆囊横切宽径",
  3042: "胆囊横切厚径",
  3050: "门静脉内径",
  3051: "门静脉内径",
  3060: "胰腺长轴横切面",
  3061: "胰体前后径",
};
export const spleen2C = {
  4001: "脾脏纵切面",
  4010: "脾脏纵切面",
  4011: "脾脏长径",
  4012: "脾脏厚径",
  4020: "脾门静脉切面",
  4021: "脾门静脉内径",
};
export const urinary2C = {
  5001: "肾脏长轴切面",
  5002: "肾脏长轴切面",
  5010: "肾切面",
  5011: "肾长轴切面",
  5012: "肾长轴血流图",
  5020: "肾切面",
  5021: "肾长轴切面",
  5022: "肾长轴血流图",
};

//比较两个对象对应字段是否相等
export function looseEqual(a, b) {
  try {
    let key = true;
    const arr = ["sNo", "name", "sex", "birth", "year", "pos", "seen", "tips"];
    arr.forEach((item) => {
      if (a[item] != b[item]) {
        key = false;
      }
    });
    return key;
  } catch (err) {
    console.log(err);
  }
}
export const stateInfo = {
  0: "等待检查",
  1: "正在检查",
  2: "暂停检查",
  3: "结束检查",
  4: "已出报告",
  5: "检查中断",
  6: "已打印",
  7: "过号",
};
export const statusOptions = [
  {
    value: "all",
    label: "所有状态",
  },
  {
    value: "0",
    label: "等待检查",
  },
  {
    value: "1",
    label: "正在检查",
  },
  {
    value: "2",
    label: "暂停检查",
  },
  {
    value: "3",
    label: "结束检查",
  },
  {
    value: "4",
    label: "已出报告",
  },
  {
    value: "5",
    label: "检查中断",
  },
  {
    value: "6",
    label: "已打印",
  },
];
export const genderOpts = [
  { label: "男", value: 1 },
  { label: "女", value: 0 },
];
export const json_fields = {
  模板分类: "subject",
  症状: "symptom",
  超声影像所见: "seen",
  超声影像诊断提示: "tips",
};
export const word_fields = {
  模板类型: "type",
  拼音码: "pinyin",
  详细内容: "detail",
};
export const modeOptions = [
  {
    value: "all",
    label: "所有模式",
  },
  {
    value: "自主模式",
    label: "自主模式",
  },
  {
    value: "远程模式",
    label: "远程模式",
  },
];
export function video2Img(video) {
  if (!video) return;
  const canvas = document.createElement("canvas");
  const w = parseInt(window.getComputedStyle(video).width);
  const h = parseInt(window.getComputedStyle(video).height);
  canvas.width = w;
  canvas.height = h;
  const context = canvas.getContext("2d");
  // 绘制当前视频帧到 canvas 上
  context.drawImage(video, 0, 0, canvas.width, canvas.height);
  return canvas.toDataURL("image/png");
}
//根据imgurl获取ip port等信息
export function getIpAndPortMore(url, port) {
  try {
    if (!url) return;
    const parsedUrl = new URL(url);
    console.log("parsedUrl", parsedUrl);

    // 获取IP和端口
    const ip = parsedUrl.hostname;
    const urlPort = parsedUrl.port;
    const baseUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}:${port}`;
    const pathname = window.decodeURIComponent(parsedUrl.pathname);

    // 使用'/'分割路径
    const pathParts = pathname.split("/").filter((part) => part); // 过滤掉空字符串

    // 获取FileName和SubDir
    let fileName = pathParts.pop();
    if (fileName) {
      fileName = fileName.replace(
        ".jpg",
        `_measure${new Date().getTime()}.jpg`
      );
    }
    const subDir = `${pathParts.join("/")}/`; // 从第二项开始

    return {
      ip: ip,
      port: port,
      SubDir: subDir,
      FileName: fileName,
      baseUrl,
      urlPort,
      param_id: fileName,
      // param_id: fileName.replace(".jpg", ``),
    };
  } catch (error) {
    console.log("getIpAndPortMore error :", error);
  }
}
//根据video url获取ip port等信息
export function getIpAndPortMore2(url, port, info) {
  try {
    if (!url) return;
    const parsedUrl = new URL(url);
    console.log("parsedUrl", parsedUrl);

    // 获取IP和端口
    const ip = parsedUrl.hostname;
    const urlPort = parsedUrl.port;
    const baseUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}:${port}`;
    const pathname = window.decodeURIComponent(parsedUrl.pathname);

    // 使用'/'分割路径
    const pathParts = pathname.split("/").filter((part) => part); // 过滤掉空字符串
    const oriFileName = pathParts.pop();

    // 获取FileName
    let fileName = createfileNameByInfo(info);
    if (!fileName) {
      fileName = oriFileName;
      fileName = fileName.replace(
        ".mp4",
        `_measure${new Date().getTime()}.jpg`
      );
    }
    // 获取ubDir
    const subDir = `${pathParts.join("/").replace("video", "images")}/`; // 从第二项开始

    return {
      ip: ip,
      port: port,
      SubDir: subDir,
      FileName: fileName,
      baseUrl,
      urlPort,
      param_id: fileName,
      // param_id: fileName.replace(".jpg", ``),
    };
  } catch (error) {
    console.log("getIpAndPortMore error :", error);
  }
}
//根据视频的节点信息生成图片名称
export function createfileNameByInfo(info) {
  try {
    let fileName = "";
    // const thyroid_map = {
    //   51: "CrossThmus",
    //   52: "CrossLeftLobe",
    //   53: "LongLeftLobeUp",
    //   62: "CrossRightLobe",
    //   63: "LongRightLobeUp",
    // };
    // const artery_map = {
    //   51: "横切颈总",
    //   52: "横切窦部",
    //   53: "横切颈内外",
    //   54: "纵切窦部",
    //   55: "纵切颈内外",
    //   56: "纵切颈总",
    //   57: "纵切起始部",
    //   61: "横切颈总",
    //   62: "横切窦部",
    //   63: "横切颈内外",
    //   64: "纵切窦部",
    //   65: "纵切颈内外",
    //   66: "纵切颈总",
    //   67: "纵切起始部",
    // }
    // const digestive_map = {}
    // const urinary_map = {}
    if (info.ScanPart.toLocaleLowerCase() == "thyroid") {
      // if (info.ScanPose) {
      //   fileName = `${
      //     thyroid_map[info.ScanPose]
      //   }_org_measure${new Date().getTime()}.jpg`;
      // } else {
      //   fileName = `CrossThmus_org_measure${new Date().getTime()}.jpg`;
      // }
      fileName = `thyroidVideo_org_measure${new Date().getTime()}.jpg`;
    } else if (info.ScanPart.toLocaleLowerCase() == "artery") {
      fileName = `auto-L-${
        info.ScanPose
      }-normal-sourceImage-1170_measure${new Date().getTime()}.jpg`;
    } else if (info.ScanPart.toLocaleLowerCase() == "digestive") {
      fileName = `auto-L-${
        info.ScanPose
      }-normal-sourceImage-1170_measure${new Date().getTime()}.jpg`;
    } else if (info.ScanPart.toLocaleLowerCase() == "urinary") {
      fileName = `auto-L-${
        info.ScanPose
      }-normal-sourceImage-1170_measure${new Date().getTime()}.jpg`;
    }
    return fileName;
  } catch (error) {
    console.log("createfileNameByInfo error :", error);
  }
}
//处理用户与医院的的name与logo
export function dellNameAndLogo(origin, userInfo, hospitalInfo) {
  origin = JSON.parse(origin);
  const user = origin.user;
  const hospital = origin.hospital;
  if (!user || !user.logo) {
    origin.user = {
      name: userInfo.name,
      logo: userInfo.sign,
    };
  }
  if (!hospital || !hospital.logo) {
    origin.hospital = {
      name: hospitalInfo.name,
      logo: hospitalInfo.logo,
    };
  }
  return {
    ...origin,
  };
}
//智能匹配时选择图片集合
export function findChoseImg(target) {
  if (!target || target.length == 0) return [];
  let res = [];
  let lesion = [];
  target.forEach((item) => {
    if (item.isChoosed) {
      if (item.status == "lesion") {
        lesion.push(item.id);
      } else {
        res.push(item.id);
      }
    }
  });
  let new_res = getRandomRes(lesion, res);
  return [...new Set([...new_res])];
}
//用于从数组中随机选择n个不同的元素
export function getRandomElementsFromArray(array, n) {
  let result = [];
  let _array = [...array]; // 复制数组，以避免修改原始数组
  while (result.length < n && _array.length > 0) {
    const index = Math.floor(Math.random() * _array.length);
    result.push(_array.splice(index, 1)[0]);
  }
  return result;
}
//从两个数组中随机选择
export function getRandomRes(lesion, res) {
  let arr = [];
  // 当两个数组长度都大于等于2时，从每个数组中随机选择两个元素
  if (lesion.length >= 2 && res.length >= 2) {
    arr = [
      ...getRandomElementsFromArray(lesion, 2),
      ...getRandomElementsFromArray(res, 2),
    ];
  } else {
    // 如果任一数组长度不足，尽可能平均地从两个数组中选择元素
    let elementsNeeded = 4;
    let lesionElements = lesion.length >= 2 ? 2 : lesion.length;
    let resElements = res.length >= 2 ? 2 : res.length;
    // 如果两个数组中的元素总和不足4个，那么只能选取它们能提供的所有元素
    elementsNeeded = Math.min(elementsNeeded, lesion.length + res.length);
    // 从lesion和res中选取元素
    arr = [
      ...getRandomElementsFromArray(
        lesion,
        Math.min(lesionElements, elementsNeeded)
      ),
      ...getRandomElementsFromArray(
        res,
        Math.min(resElements, elementsNeeded - lesionElements)
      ),
    ];
  }
  return arr;
}
//两个数组去重
export function combineAndUnique(target1, target2) {
  if (!target1 || !target2) return [];
  if (target1.length > 4) {
    return [...new Set([...target1])];
  }
  return [...new Set([...target1, ...target2])];
}
//自定义四舍五入
export function customToFixed(number, decimalPlaces) {
  if (typeof number !== "number") {
    throw new Error("Number must be a valid number.");
  }

  if (typeof decimalPlaces !== "number") {
    throw new Error("Decimal places must be a valid number.");
  }

  if (decimalPlaces < 0) {
    throw new Error("Decimal places must be non-negative.");
  }

  const multiplier = Math.pow(10, decimalPlaces);
  const roundedNumber = Math.round(number * multiplier) / multiplier;

  return roundedNumber.toFixed(decimalPlaces);
}
export const logColumns = [
  {
    title: "操作时间",
    dataIndex: "createdAt",
    key: "createdAt",
    ellipsis: true,
    width: 160,
  },
  {
    title: "用户名",
    dataIndex: "name",
    key: "name",
    ellipsis: true,
  },
  {
    title: "类别",
    key: "type",
    dataIndex: "type",
    ellipsis: true,
  },
  {
    title: "操作事件",
    key: "event",
    dataIndex: "event",
    ellipsis: true,
    width: 160,
  },
];
//根据部位及是否有病灶来判断模板的标题
export function getSymptomTitleByPos(pos, hasLesion) {
  let title = "";
  if (hasLesion) {
    if (pos == "甲状腺") {
      title = `甲状腺结节`;
    }
    if (pos == "颈动脉") {
      title = `颈动脉斑块`;
    }
    if (pos == "泌尿" || pos == "泌尿系") {
      title = `泌尿常见病变`;
    }
    if (pos == "腹部" || pos == "消化系") {
      title = `肝常见病变`;
    }
  } else {
    if (pos == "甲状腺") {
      title = `正常甲状腺`;
    }
    if (pos == "颈动脉") {
      title = `正常颈动脉`;
    }
    if (pos == "泌尿" || pos == "泌尿系") {
      title = `泌尿正常`;
    }
    if (pos == "腹部" || pos == "消化系") {
      title = `正常腹腔`;
    }
  }
  return title;
}
export function get2RandomIdByImgList(imgs) {
  const len = imgs.length;
  const newId = [];
  const random1 = Math.floor(Math.random() * len);
  const random2 = Math.floor(Math.random() * len);
  newId.push(imgs[random1].param_id, imgs[random2].param_id);
  return newId;
}
export const arteryCali2C = {
  19:"-颈总内中膜厚度",
  33:"-颈总内中膜厚度",
  26:"-颈总PSV和EDV",
  40:"-颈总PSV和EDV",
}
export const thyroidCali2C = {
  CrossThmus:"峡部前后径",
  CrossLeftLobe:"左侧叶前后径、左右径",
  CrossRightLobe:"右侧叶前后径、左右径"
}
export const urinaryCali2C = {
  5010:"肾上下径、前后径",
  5020:"肾上下径、前后径"
}
export const liverCali2C = {
  3010: "肝左叶上下径、厚径(前后径)",
  3020: "肝右叶斜径",
  3050: "门静脉主干内径",
  3030: "胆囊长径、短径",
  3060: "胰体前后径"
}
export const spleenCali2C = {
  4010: "脾脏长径、厚径",
  4020: "脾门部脾静脉直径",
};
// 通用函数，根据数组和当前索引以及方向找到下一个可见的图片索引
export function findNextVisibleIndex(images, currentIndex, direction) {
  const step = direction; // 方向，1为向右，-1为向左
  let nextIndex = currentIndex;

  while (nextIndex >= 0 && nextIndex < images.length) {
    nextIndex += step;

    // 检查是否越界
    if (nextIndex < 0) {
      nextIndex = 0;
      break;
    } else if (nextIndex >= images.length) {
      nextIndex = images.length - 1;
      break;
    }

    // 如果找到可见的图片，则返回索引
    if (images[nextIndex]?.visable) {
      break;
    }
  }
  return nextIndex;
}
export const  arteryCaliShow = ["左-颈总PW模式","左-颈总内中膜厚度","左-颈总PSV和EDV","右-颈总PW模式","右-颈总内中膜厚度","右-颈总PSV和EDV"]